/*	kd:
	Shop info like this is handled in the items themselves now. I'm not exactly
	sure how zs effectively stores asset data, so I'll just use a virtual for
	now, which will return correct tokens based on class, not instance. */

/* class SharedTome : Inventory {
	class <Inventory> m_token;
	
	property Token: m_token;
} */

/* class TogglerItem : Inventory {
	class <Inventory> m_token;
	property Token : m_token;
} */

/*	kd:
	This struct is contained within the player. If you want to communicate
	something like setting a spell, you should do this here, because this makes
	sure that cycling, binding and playing the correct sounds is
	all in order. */

struct Gc_SpellCycle play {
	uint m_spell;
	Gc_SpecialInvHandler m_handler;
	Actor m_user;
	
	void Init (Actor user) {
		m_handler = Gc_SpecialInvHandler(Gc_SpecialInvHandler.Find("Gc_SpecialInvHandler"));
		m_user    = user;
	}
	
	/*	Allows cycling through the spells list automatically. Note this doesn't
		update the spell on its own.
		
		It returns whether the spell changed. Then you can do something with
		that instead. To update the user's spell, call ActivateCycledSpell. */
	
	bool Next () {
		if(!m_handler || !m_user) {
			console.printf("WHY");
			return false;
		}
		
		int current = m_spell;
		
		do {
			m_spell++;
			
			if(m_handler.m_size <= m_spell) {
				m_spell -= m_handler.m_size;
			}
		} while(m_spell != current && !m_handler.GotSpellIndex(m_user, m_spell));
		
		if(m_spell != current) {
			OnSpellChange();
			return true;
		}
		
		return false;
	}
	
	bool Prev () {
		if(!m_handler || !m_user) {
			console.printf("WHY");
			return false;
		}
		
		int current = m_spell;
		
		do {
			m_spell--;
			
			if(m_spell < 0) {
				m_spell += m_handler.m_size;
			}
		} while(m_spell != current && !m_handler.GotSpellIndex(m_user, m_spell));
		
		if(m_spell != current) {
			OnSpellChange();
			return true;
		}
		
		return false;
	}
	
	bool SetToSpell (class <SharedTome> spell) {
		if(!m_handler || !m_user) {
			return false;
		}
		
		int i = m_handler.IndexForSpell(spell);
		
		if(0 <= i) {
			int previous = m_spell;
			m_spell = i;
			
			if(i != previous) {
				OnSpellChange();
				return true;
			}
		}
		
		return false;
	}
	
	// TODO:
	// I'd steal all the functions about setting the spells from the event
	// handler below and put them here, because at this point, I realised it's
	// pretty much nonsense. After doing that, the event handler will be merely
	// a good ol' data container.
	
	void OnSpellChange () {
		if(!m_handler || !m_user) {
			return;
		}
		
		m_handler.SetSpellIndex(m_user, m_spell);
		m_handler.PlaySpellChimeIndex(m_user, m_spell);
	}
}

/*	kd:
	One issue used to be that we had to manually manage a list of spells. This
	is now dealt with by using a base class to inherit from. After that, spells
	are automatically added into the list. */

class Gc_SpecialInvHandler : EventHandler {
	protected Array <class <SharedTome> >    m_spell_tomes;
	protected Array <class <Inventory> >     m_spell_tokens;
	protected bool m_is_init;
	uint m_size;
	
	/*class <SharedTome> ActiveSpell (Actor user) {
		uint size = m_spell_tomes.size();
		
		for(uint i = 0; i < size; i++) {
		}
		
		return NULL;
	}*/
	
	int IndexForSpell (class <SharedTome> spell) {
		int i = m_spell_tomes.Find(spell);
		
		if(0 < m_size && i != m_size) {
			return i;
		}
		
		return -1;
	}
	
	bool GotSpellIndex (Actor user, uint spell) {
		if(user && spell < m_size) {
			return user.CountInv(m_spell_tomes [spell]);
		}
		
		return false;
	}
	
	void SetSpellIndex (Actor user, uint spell) {
		if(spell < m_size) {
			SetSpell(user, m_spell_tomes [spell]);
		}
	}
	
	/*	Spells work by having exactly one of a certain spell token in your
		inventory, so when we set a spell, we want to take away all tokens, and
		give one back. */
	void SetSpell (Actor user, class <SharedTome> spell) {
		if(0 < user.CountInv(spell)) {
			uint size = m_spell_tomes.size();
			
			for(uint i = 0; i < size; i++) {
				user.TakeInventory(GetDefaultByType(m_spell_tomes [i]).m_token,
						0x7fffffff);
			}
			
			user.GiveInventory(GetDefaultByType(spell).m_token, 1);
		}
	}
	
	void PlaySpellChimeIndex (Actor source, uint index) {
		if(source && index < m_size) {
			source.A_PlaySound(SpellSound(m_spell_tomes [index]));
		}
	}
	
	void PlaySpellChime (Actor source, class <SharedTome> spell) {
		if(source) {
			source.A_PlaySound(SpellSound(spell));
		}
	}
	
	String SpellSound (class <SharedTome> spell) {
		return String.Format("%s_active", spell.GetClassName());
	}
	
	/*	Setup. */
	protected void SortActorTypes () {
		uint size = AllActorClasses.size();
		
		for(uint i = 0; i < size; i++) {
			if(AllActorClasses [i] is 'SharedTome') {
				if(AllActorClasses [i].GetClassName() == "SharedTome") {
					continue;
				}
				
				class <SharedTome> tome = (class <SharedTome>)(AllActorClasses [i]);
				m_spell_tomes.push(tome);
				// m_spell_tokens.push(tome.Token());
				m_size++;
			}
		}
		
		// PrintAllTomes();
	}
	
	void PrintAllTomes () {
		for(uint i = 0; i < m_spell_tomes.size(); i++) {
			Console.printf("%s", m_spell_tomes [i].GetClassName());
		}
	}
	
	override void OnRegister () {
		if(!m_is_init) {
			SortActorTypes();
			m_is_init = true;
		}
	}
}

/*	Probably now useless. */

class Gc_InvMan play {
	/*	Turn inventory on / off. */
	static void ToggleInv (Actor user, class <Inventory> inv) {
		if(0 < user.CountInv(inv)) {
			user.TakeInventory(inv, 0x7fffffff);
		}
		
		else {
			user.GiveInventory(inv, 1);
		}
	}
}